package queue

import (
	"errors"
    
    "collecter/app/common/helper"
    "collecter/app/queue/driver"
)

/**
*  Push
*  @Description: 推送消息到队列服务提供者（根据配置自动适配不同的队列服务提供者）
*  @param queueName 队列名称
*  @param taskName 任务名称
*  @param params 任务参数
*  @param retryTimes 重试次数
*  @return error
 */
func Push(queueName string, taskName string, params string, retryTimes int64) error {
	isEnableQueue := helper.Configer.GetBool("isEnableQueue")
	if !isEnableQueue {
		return nil
	}
	if len(queueName) == 0 || len(taskName) == 0 || retryTimes < 0 {
		return errors.New("参数错误")
	}
	defaultDriver := helper.Configer.GetString("queue.defaultDriver")

	provider := providerMap[defaultDriver]
	if err := provider.Connect(); err != nil {
		return err
	}
	instance := provider.SetValue(queueName, taskName, params, retryTimes, 0, "", 0, 0, "")
	defer func() { instance = nil }()
	return instance.Push()
}

/**
*  PushDelay
*  @Description: 延迟推送消息到队列服务提供者（根据配置自动适配不同的队列服务提供者）
*  @param queueName 队列名称
*  @param taskName 任务名称
*  @param params 任务参数
*  @param retryTimes 重试次数
*  @param delays 延迟时间，单位秒
*  @return error
 */
func PushDelay(queueName string, taskName string, params string, retryTimes int64, delays int64) error {
	isEnableQueue := helper.Configer.GetBool("isEnableQueue")
	if !isEnableQueue {
		return nil
	}
	if len(queueName) == 0 || len(taskName) == 0 || retryTimes < 0 {
		return errors.New("参数错误")
	}
	defaultDriver := helper.Configer.GetString("queue.defaultDriver")

	provider := providerMap[defaultDriver]
	if err := provider.Connect(); err != nil {
		return err
	}
	instance := provider.SetValue(queueName, taskName, params, retryTimes, 0, "", 0, delays, "")
	defer func() { instance = nil }()
	return instance.Push()
}

/**
*  Pull
*  @Description: 根据配置从当前的队列服务提供者拉取消息
*  @param queueName 队列名称
*  @param timeOut 超时时间
*  @return *queues.TaskStruct
*  @return error
 */
func Pull(queueName string, timeOut int64) (*driver.MessageStruct, error) {
	var taskInfo *driver.MessageStruct
	isEnableQueue := helper.Configer.GetBool("isEnableQueue")
	if !isEnableQueue {
		return taskInfo, nil
	}
	if len(queueName) == 0 {
		return taskInfo, errors.New("队列名称不能为空")
	}

	defaultDriver := helper.Configer.GetString("queue.defaultDriver")
	if err := providerMap[defaultDriver].Connect(); err != nil {
		return taskInfo, err
	}
	instance := providerMap[defaultDriver].SetValue("", "", "", 0, 0, queueName, timeOut, 0, "")
	defer func() { instance = nil }()
	return instance.Pull()
}

func DeleteTask(queueName string, taskName string) error {
	isEnableQueue := helper.Configer.GetBool("isEnableQueue")
	if !isEnableQueue {
		return nil
	}
	if len(queueName) == 0 || len(taskName) == 0 {
		return errors.New("参数错误")
	}
	defaultDriver := helper.Configer.GetString("queue.defaultDriver")
	if err := providerMap[defaultDriver].Connect(); err != nil {
		return err
	}
	instance := providerMap[defaultDriver].SetValue(queueName, taskName, "", 0, 0, "", 0, 0, "")
	defer func() { instance = nil }()
	return instance.DeleteTask()
}

func Confirm(taskName string, params string, retryTimes int64, retryedTimes int64, state string) error {
	isEnableQueue := helper.Configer.GetBool("isEnableQueue")
	if !isEnableQueue {
		return nil
	}
	if len(taskName) == 0 || retryedTimes < 0 {
		return errors.New("参数错误")
	}
	defaultDriver := helper.Configer.GetString("queue.defaultDriver")

	provider := providerMap[defaultDriver]
	if err := provider.Connect(); err != nil {
		return err
	}
	instance := provider.SetValue("", taskName, params, retryTimes, retryedTimes, "", 0, 0, state)
	defer func() { instance = nil }()
	return instance.Comfirm()
}

func Info(queueName string) (map[string]int64, error) {
	rst := make(map[string]int64)
	isEnableQueue := helper.Configer.GetBool("isEnableQueue")
	if !isEnableQueue {
		return rst, nil
	}
	if len(queueName) == 0 {
		return rst, errors.New("参数错误")
	}

	defaultDriver := helper.Configer.GetString("queue.defaultDriver")
	if err := providerMap[defaultDriver].Connect(); err != nil {
		return rst, err
	}
	instance := providerMap[defaultDriver].SetValue("", "", "", 0, 0, queueName, 0, 0, "")
	defer func() { instance = nil }()
	return instance.Info()
}
